package com.yootk.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;

import java.util.Map;
import java.util.Properties;

/**
 * PropertySources属性源管理
 */
public class Demo02_PropertySources {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo02_PropertySources.class);

    public static void main(String[] args) {
        test1();
    }

    /**
     * 实现多个属性源的管理
     */
    public static void test1() {
        // 属性源1
        Map<String, Object> data = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com");
        PropertySource mapSource = new MapPropertySource("url", data);
        // 属性源2
        Properties prop = new Properties();
        prop.put("java", "Java进阶开发实战");
        prop.put("spring", "Spring开发实战");
        PropertySource propSource = new PropertiesPropertySource("book", prop);
        // 追加属性源管理
        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(mapSource);
        sources.addLast(propSource);
        // 1、先获取对应的属性源；2、再进行资源的查找
        LOGGER.info("属性资源获取：java = {}", sources.get("book").getProperty("java"));
    }
}
